<template>
  <div style="position: relative">
    <el-dialog center :visible.sync="deflag" title="确认删除" width="30%">
      <div slot="footer" class="dialog-footer" style="width: 100%;display: flex;justify-content: space-between">
        <el-button @click="deflag = false">取 消</el-button>
        <el-button type="primary" @click="DelThis">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="dialogTitle"
      center
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <div style="width: 100%;overflow: auto;height: 50vh">
        <el-form ref="userForm" :model="formData" label-width="80px">
          <el-form-item label="题目" prop="title">
            <el-input v-model="formData.title"></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="overview">
            <el-input v-model="formData.overview" type="textarea" :rows="6" maxlength="250" :show-word-limit="true"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer" style="width: 100%;display: flex;justify-content: space-between">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveTopic">确 定</el-button>
        </div>
      </div>
    </el-dialog>
    <el-button type="primary" style="margin-left: 90%; margin-top: 2vh; margin-bottom: 2vh" @click="openDialog('add',null)">新增题目</el-button>
    <el-table width="100%" height="70vh" :data="List">
      <el-table-column label="序号"  align="center">
        <template slot-scope="scope">
          {{(currentPage-1)*pageSize+scope.$index+1}}
        </template>
      </el-table-column>
      <el-table-column label="题目" prop="title" align="center"></el-table-column>
      <el-table-column label="描述" prop="overview" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="info" size="mini" @click="openDialog('edit',scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" v-if="judge" @click="ShowDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <center>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </center>
  </div>
</template>

<script>
import { GetList, Remove, SaveTopic } from '@/api/topic'

export default {
  name: 'Topic',
  data() {
    return {
      dialogVisible: false,
      dialogTitle: '',
      List: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      formData: {
        id:null,
        title:"",
        overview:"",
        isselect:0
      },
      id:"",
      deflag:false
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    DelThis(){
      let that=this
      Remove(this.id).then(()=>{
        that.currentPage=1
        that.getList()
        that.deflag=false
        this.$message.success("删除成功")
      })
    },
    ShowDel(id){
      this.id=id
      this.deflag=true
    },
    judge(){
      return localStorage.getItem('role') === '管理员';
    },
    getList() {
      const params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize
      };
      GetList(params).then(res => {
        this.List = res.topicPage.records;
        this.total = res.topicPage.total;
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
      this.$message.success("已修改")
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    openDialog(mode, data) {
      this.dialogTitle = mode === 'add' ? '新增题目' : '编辑题目';
      this.dialogVisible = true;
      if (mode === 'edit' && data) {
        this.formData = { ...data };
      } else {
        // Reset form data for add mode
        this.$refs.userForm.resetFields();
        this.formData = {
          id:null,
          title:"",
          overview:"",
          isselect:0
        }
        this.$message.success("保存成功")
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    saveTopic() {
      SaveTopic(this.formData).then(res=>{
        this.currentPage=1
        this.getList()
      })
      // After saving, you might want to close the dialog
      this.dialogVisible = false;
      this.$message.success("保存成功")
    }
  }
}
</script>

<style scoped lang="scss">
.avatar-uploader {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 150px;
  height: 150px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  overflow: hidden;
}

.avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
}
</style>
